package com.iteaj.framework.spring.boot;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * create time: 2020/3/5
 *  增加激活framework、rpc、mybatis等配置文件
 * @author iteaj
 * @since 1.0
 */
public class FrameworkEnvironmentPreparedListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        ConfigurableEnvironment environment = event.getEnvironment();
        environment.setActiveProfiles("framework", "rpc");

        if(WebApplicationType.NONE == event.getSpringApplication().getWebApplicationType()) {
            environment.addActiveProfile("mybatis");
        }

    }

    /**
     * 启动顺序必须早于ConfigFileApplicationListener
     * @see org.springframework.boot.context.config.ConfigFileApplicationListener
     * @return
     */
    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE + 9;
    }
}
